/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPCommand;
import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFactory;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPParams;

/**
 * GWT tests for OPCommand.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 09 December 2010
 */
public class OPCommandTestGwt extends GWTTestCase {

    private static final String SERVICE_NAME = "dummyServiceName";
    private static final String PARAM_NAME = "dummyParamName";
    private static final String PARAM_VALUE = "dummyParamValue";
    private static final int COMMAND_TYPE = OPCommandFactory.LOAD_PART;

    private OPCommand command;
    private OPCommandCallback callback;
    private OPParams params;

    private int onSuccessCalled = 0;
    private int onFailCalled = 0;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called at the start of each test to set up the action.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        params = new OPParams(PARAM_NAME, PARAM_VALUE);
        callback = new OPCommandCallback() {
            @Override
            public void onFail(final OPCommandFailureReason reason) {
                ++onFailCalled;
            }

            @Override
            public void onSuccess() {
                ++onSuccessCalled;
            }
        };
        command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, params, callback);
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        onSuccessCalled = 0;
        onFailCalled = 0;
    }

    /**
     * Test creating a command. Should get something not null.
     */
    public final void testCreatingCommand() {
        assertNotNull("command shouldn't be null", command);
    }
    
    /**
     * Test trying to create a command with a null service. Should NOT raise an error.
     */
    public final void testCreatingCommandWithNullService() {
        try {
            command = OPCommand.commandFactory(COMMAND_TYPE, null, params, callback);
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when trying to build a command with null service.");
        }
    }

    /**
     * Test trying to create a command with a null parameter. Should NOT raise an error.
     */
    public final void testCreatingCommandWithNullParameters() {
        try {
            command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, null, callback);
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when trying to build a command with null parameters.");
        }
    }

    /**
     * Test trying to create a command with a null callback. Should NOT raise an error.
     */
    public final void testCreatingCommandWithNullCallback() {
        try {
            command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, params, null);
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when trying to build a command with a null callback.");
        }
    }

    /**
     * Test getting back a command type.
     */
    public final void testGettingType() {
        assertEquals("Command type should be a load command", COMMAND_TYPE, command.getType());
    }
    
    /**
     * Test getting back a command service.
     */
    public final void testGettingService() {
        assertEquals("Command service should be " + SERVICE_NAME, SERVICE_NAME, command.getService());
    }

    /**
     * Test getting back a null service.
     */
    public final void testGettingNullService() {
        command = OPCommand.commandFactory(COMMAND_TYPE, null, params, callback);
        assertNull("Command service should be null", command.getService());
    }

    /**
     * Test setting a service.
     */
    public final void testSettingService() {
        command.setService(SERVICE_NAME + "2");
        assertEquals("Command service should be " + SERVICE_NAME + "2", SERVICE_NAME + "2", command.getService());
    }
    
    /**
     * Test setting a null service. Should NOT raise an error.
     */
    public final void testSettingNullService() {
        try {
            command.setService(null);
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when trying to set a null service");
        }
    }
    
    /**
     * Test setting callback.
     */
    public final void testSettingCallback() {
        command.setCallback(new OPCommandCallback() {
            @Override
            public void onSuccess() {
                ++onFailCalled;
            }
            @Override
            public void onFail(final OPCommandFailureReason reason) {
                ++onSuccessCalled;
            }
        });
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
        command.onFail(OPCommandFailureReason.commandFailureReasonFactory(0, null));
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
    }
    
    /**
     * Test setting a null callback. Should NOT raise an error.
     */
    public final void testSettingNullCallback() {
        try {
            command.setCallback(null);
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when trying to set a null callback");
        }
    }
    
    /**
     * Test getting back a command params.
     */
    public final void testGettingParams() {
        OPParams parameters = command.getParams();
        assertNotNull("Command params shouldn't be null", parameters);
        assertEquals("Command params should contains one element", 1, parameters.size());
        assertEquals("Command params should contains one value", PARAM_VALUE, parameters.getParamValue(PARAM_NAME));
    }

    /**
     * Test getting back a null params.
     */
    public final void testGettingNullParams() {
        command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, null, callback);
        assertNull("Command params should be null", command.getParams());
    }

    /**
     * Test calling onSuccess method. Should call the callback onSuccess method.
     */
    public final void testCallingOnSuccess() {
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onSuccess();
        assertEquals("onSuccess should have been called now", 1, onSuccessCalled);
        assertEquals("onFail should still not have been called", 0, onFailCalled);
    }

    /**
     * Test calling onFail method. Should call the callback onFail method.
     */
    public final void testCallingOnFail() {
        assertEquals("onSuccess should not have been called yet", 0, onSuccessCalled);
        assertEquals("onFail should not have been called yet", 0, onFailCalled);
        command.onFail(OPCommandFailureReason.commandFailureReasonFactory(0, null));
        assertEquals("onSuccess should still not have been called", 0, onSuccessCalled);
        assertEquals("onFail should have been called now", 1, onFailCalled);
    }

    /**
     * Test calling onSuccess method with a null callback. Should NOT raise an error.
     */
    public final void testCallingOnSuccessWithNullCallback() {
        try {
            command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, params, null);
            command.onSuccess();
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when calling onSuccess() with a null callback.");
        }
    }

    /**
     * Test calling onFail method with a null callback. Should NOT raise an error.
     */
    public final void testCallingOnFailWithNullCallback() {
        try {
            command = OPCommand.commandFactory(COMMAND_TYPE, SERVICE_NAME, params, null);
            command.onFail(OPCommandFailureReason.commandFailureReasonFactory(0, null));
        } catch (Throwable e) {
            fail("OPCommand should NOT raise an error when calling onFail() with a null callback.");
        }
    }

}
